package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyClientTransferRequestTypeAdvanced
        extends AbstractHandleSocksReportClientProxyClientTransferRequestTypeAdvanced<NettyProxyMsg> {

    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleSocksReportClientProxyClientTransferRequestTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     * @see NettySocksClientProxyServerRealHandler
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel transferChannel = nettyChannelContext.channel();


        Integer targetPort = ChannelAttributeKeyUtils.getTargetPort(transferChannel);
        String targetIp = ChannelAttributeKeyUtils.getTargetIp(transferChannel);
        String clientId = ChannelAttributeKeyUtils.getClientId(transferChannel);

        byte[] visitorId = nettyProxyMsg.getVisitorId();
        // 目标通道
        Channel nextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(transferChannel);

        NettyProxyMsg requestMsg = new NettyProxyMsg();
        requestMsg.setVisitorId(visitorId);
        requestMsg.setData(nettyProxyMsg.getData());
        requestMsg.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_REQUEST_);
        if (nextChannel != null) {
            nextChannel.writeAndFlush(requestMsg);
            // 记录进口数据
            ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                    .builder()
                    .channelFlowEnum(ChannelFlowEnum.IN_FLOW)
                    .port(targetPort)
                    .ip(targetIp)
                    .clientId(clientId)
                    .channelProxyType(ChannelProxyType.CLIENT_PROXY_CLIENT)
                    .channelProtocolType(ChannelProtocolType.SOCKS)
                    .flow(nettyProxyMsg.getData().length)
                    .build();
            channelProxyFlowAdapter.asyncHandler(transferChannel, serverChannelFlow);
        } else {
            log.error("can not find the  channel");
        }


    }

}
